﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Presentation.Commands;

namespace StockholmsStad.Windows.UI.Modules.Couple.Models
{
    public class ExtrasPresentationModel : INotifyPropertyChanged, IDataErrorInfo
    {
        private readonly IDictionary<string, string> errors = new Dictionary<string, string>();

        public event EventHandler<DataEventArgs<ExtrasPresentationModel>> Saved;
        public DelegateCommand<object> SaveCommand { get; private set; }

        public ExtrasPresentationModel()
        {
            SaveCommand = new DelegateCommand<object>(Save, CanSave);
        }

        private bool CanSave(object obj)
        {
            return errors.Count == 0;
        }

        private void Save(object obj)
        {
            if (CanSave(obj))
                OnSaved(new DataEventArgs<ExtrasPresentationModel>(this));
        }

        private void OnSaved(DataEventArgs<ExtrasPresentationModel> e)
        {
            var savedHandler = Saved;

            if (savedHandler != null)
                savedHandler(this, e);
        }

        #region Properties

        public string HeaderInfo
        {
            get { return HeaderNames.Extras; }
        }

        public int? TicketId
        {
            get; set;
        }

        public bool IsInChangeMode { get; set; }

        private string extrasInfo;
        public string ExtrasInfo
        {
            get { return extrasInfo; }
            set 
            {
                if (!value.Equals(extrasInfo))
                {
                    extrasInfo = value.Trim();
                    
                    if(PropertyChanged != null)
                        PropertyChanged(this, new PropertyChangedEventArgs("ExtrasInfo"));
                }
            }
        }

        #endregion

        #region ErrorHandling and Validation

        public string this[string columnName]
        {
            get
            {
                if (errors.ContainsKey(columnName))
                    return errors[columnName];

                return null;
            }
            set
            {
                errors[columnName] = value;
            }
        }

        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region IDataErrorInfo Members

        public string Error
        {
            get { throw new NotImplementedException(); }
        }

        #endregion
    }
}
